package com.hdoit.kylin.web.baseInfo.controllers;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.tennetcn.common.enums.ManageContainer;
import com.tennetcn.common.enums.ModelStatus;
import com.tennetcn.common.enums.pub.SexStatus;
import com.tennetcn.common.logical.superbase.service.IEmployeeService;
import com.tennetcn.common.message.PagerModel;
import com.tennetcn.common.utils.ChineseUtils;
import com.tennetcn.common.utils.CommonUtils;
import com.tennetcn.common.utils.DateUtils;
import com.tennetcn.component.web.ActionResult;
import com.tennetcn.component.web.BaseController;
import com.tennetcn.component.web.ErrorResult;
import com.tennetcn.component.web.JsonActionResult;
import com.hdoit.kylin.logical.baseInfo.model.Operation;
import com.hdoit.kylin.logical.baseInfo.service.IOperationService;
import com.hdoit.kylin.logical.viewmodel.baseInfo.OperationSearch;

/**
 * @author      lufu
 * @email       
 * @createtime  2017年07月27日 16:52:21
 * @comment
 */
@Controller
@RequestMapping(value = "/admin/baseInfo/operation/")
public class OperationController extends BaseController{
    @Override
    public String getRoutePath() {
        return "areas/admin/baseInfo/operation/";
    }

    private IEmployeeService employeeService;
    public IEmployeeService getEmployeeService() {
		return employeeService;
	}
    @Resource
	public void setEmployeeService(IEmployeeService employeeService) {
		this.employeeService = employeeService;
	}

	private IOperationService operationService;
    public IOperationService getOperationService() {
        return operationService;
    }
    @Resource
    public void setOperationService(IOperationService operationService) {
        this.operationService = operationService;
    }

    private final String indexViewName="index";
    private final String outputViewName="output";
    private final String detailedViewName="operationDetailed";
    private final String saveViewName="save";
    private final String deleteViewName = "delete";
    private final String checkAccountViewName="checkAccount";

    @RequestMapping(value = indexViewName, method = { RequestMethod.GET,RequestMethod.POST })
    public String index(Model model,PagerModel pagerModel,OperationSearch search,boolean ajaxPost){

        pagerModel.setTotalAmount(operationService.queryCount(search));
        List<Operation> operations= operationService.queryList(search,pagerModel);

        model.addAttribute(ManageContainer.PAGERKEY, pagerModel);
        model.addAttribute("operations", operations);
        model.addAttribute("operationSearch", search);

        if(ajaxPost){
            return view(outputViewName);
        }

        return view(indexViewName);
    }

    @RequestMapping(value = detailedViewName, method = { RequestMethod.GET})
    public String operationDetailed(Model model,String key){
        Operation operation;
        if(StringUtils.isEmpty(key)){
        	operation=new Operation();
        	operation.setSex(SexStatus.male);
        }
        else{
        	operation=operationService.queryModel(key);
        }
        model.addAttribute("operation", operation);
        return view(detailedViewName);
    }

    @RequestMapping(value = saveViewName, method = { RequestMethod.POST})
    public String save(Model model,Operation operation){
        if(StringUtils.isEmpty(operation.getEmployeeId())){
            operation.setEmployeeId(CommonUtils.getKeyId());
            operation.setOperationId(CommonUtils.getKeyId());
            operation.setEmployeeName(operation.getOperationName());
            operation.setCreateDate(DateUtils.getCurrent());
            /*operation.setDeleteMark(YesOrNoInteger.no);*/
            operation.setCreateUserId(getLoginModel().getLoginId());
            operation.setCreateUserName(getLoginModel().getLoginName());
            operation.setModelStatus(ModelStatus.add);
        }else{
        	operation.setEmployeeName(operation.getOperationName());
            operation.setModifyDate(DateUtils.getCurrent());
            operation.setModifyUserId(getLoginModel().getLoginId());
            operation.setModifyUserName(getLoginModel().getLoginName());
            operation.setModelStatus(ModelStatus.update);
        }
        operation.setEmployeeCode(ChineseUtils.getPingYin(operation.getEmployeeName()));
        operation.setMarkCode(ChineseUtils.getPinYinHeadChar(operation.getEmployeeName()));

        ActionResult actionResult;
        if(operationService.applyChange(operation)){
            actionResult=new ActionResult();
        }else{
            actionResult=new ErrorResult();
        }

        model.addAttribute("operation", operation);
        model.addAttribute(ManageContainer.RESULTKEY, actionResult);

        return view(detailedViewName);
    }

    @RequestMapping(value =deleteViewName, method = { RequestMethod.POST },produces="text/plain;charset=utf-8")
    public @ResponseBody String delete(String key){
        if(operationService.deleteModel(key)&&employeeService.deleteModel(key)){
            return new JsonActionResult(ManageContainer.SUCCESS).toJson();
        }
        return new JsonActionResult(ManageContainer.ERROR).toJson();
    }
    
    @RequestMapping(value = checkAccountViewName, method = { RequestMethod.GET })
	public @ResponseBody String checkAccount(String employeeId,String employeeAccount){
		return String.valueOf(!employeeService.checkAccount(employeeId, employeeAccount));
	}
}